﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Stocks;
using System.Data.SqlClient;
using System.Data;
using ChangeHope.DataBase;

namespace YXShop.SQLServerDAL.Stocks
{
    class Order:IOrder
    {
        #region IOrder 成员
        #region DataBase Operation
        public int Add(Model.Stocks.Order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_storeorder(");
            strSql.Append("ordernum,storeid,ordertime,adminid,isMember,Memberid,State)");
            strSql.Append(" values (");
            strSql.Append("@ordernum,@storeid,@ordertime,@adminid,@isMember,@Memberid,@State)");
            SqlParameter[] parameters = {
					new SqlParameter("@ordernum", SqlDbType.VarChar,20),
					new SqlParameter("@storeid", SqlDbType.Int,4),
					new SqlParameter("@ordertime", SqlDbType.DateTime),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@isMember", SqlDbType.Bit,1),
					new SqlParameter("@Memberid", SqlDbType.Int,4),
            	new SqlParameter("@State", SqlDbType.Int,4)};
            parameters[0].Value = model.Ordernum;
            parameters[1].Value = model.Storeid;
            parameters[2].Value = model.Ordertime;
            parameters[3].Value = model.Adminid;
            parameters[4].Value = model.isMember;
            parameters[5].Value = model.Memberid;
            parameters[6].Value = model.State;

            object obj = SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(Model.Stocks.Order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_storeorder set ");
            strSql.Append("ordernum=@ordernum,");
            strSql.Append("storeid=@storeid,");
            strSql.Append("ordertime=@ordertime,");
            strSql.Append("adminid=@adminid,");
            strSql.Append("isMember=@isMember,");
            strSql.Append("Memberid=@Memberid");
            strSql.Append("State=@State");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@ordernum", SqlDbType.VarChar,20),
					new SqlParameter("@storeid", SqlDbType.Int,4),
					new SqlParameter("@ordertime", SqlDbType.DateTime),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@isMember", SqlDbType.Bit,1),
					new SqlParameter("@Memberid", SqlDbType.Int,4),
            	new SqlParameter("@State", SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Ordernum;
            parameters[2].Value = model.Storeid;
            parameters[3].Value = model.Ordertime;
            parameters[4].Value = model.Adminid;
            parameters[5].Value = model.isMember;
            parameters[6].Value = model.Memberid;
            parameters[7].Value = model.State;

            return SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_storeorder ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_storeorder] where id in(");
            strSql.Append(" " + ids + " )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }
        #endregion
        #region DataBase Load
        public Model.Stocks.Order GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,ordernum,storeid,ordertime,adminid,isMember,Memberid,State from yxs_storeorder ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.Stocks.Order model = new Model.Stocks.Order();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Ordernum = ds.Tables[0].Rows[0]["ordernum"].ToString();
                if (ds.Tables[0].Rows[0]["storeid"].ToString() != "")
                {
                    model.Storeid = int.Parse(ds.Tables[0].Rows[0]["storeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ordertime"].ToString() != "")
                {
                    model.Ordertime = DateTime.Parse(ds.Tables[0].Rows[0]["ordertime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adminid"].ToString() != "")
                {
                    model.Adminid = int.Parse(ds.Tables[0].Rows[0]["adminid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isMember"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isMember"].ToString() == "1") || (ds.Tables[0].Rows[0]["isMember"].ToString().ToLower() == "true"))
                    {
                        model.isMember = true;
                    }
                    else
                    {
                        model.isMember = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Memberid"].ToString() != "")
                {
                    model.Memberid = int.Parse(ds.Tables[0].Rows[0]["Memberid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public Model.Stocks.Order GetModelByOrdernume(string ordernum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,ordernum,storeid,ordertime,adminid,isMember,Memberid,State from yxs_storeorder ");
            strSql.Append(" where ordernum=@ordernum ");
            SqlParameter[] parameters = {
					new SqlParameter("@ordernum", SqlDbType.VarChar,20)};
            parameters[0].Value = ordernum;

            Model.Stocks.Order model = new Model.Stocks.Order();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.Ordernum = ds.Tables[0].Rows[0]["ordernum"].ToString();
                if (ds.Tables[0].Rows[0]["storeid"].ToString() != "")
                {
                    model.Storeid = int.Parse(ds.Tables[0].Rows[0]["storeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ordertime"].ToString() != "")
                {
                    model.Ordertime = DateTime.Parse(ds.Tables[0].Rows[0]["ordertime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["adminid"].ToString() != "")
                {
                    model.Adminid = int.Parse(ds.Tables[0].Rows[0]["adminid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isMember"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isMember"].ToString() == "1") || (ds.Tables[0].Rows[0]["isMember"].ToString().ToLower() == "true"))
                    {
                        model.isMember = true;
                    }
                    else
                    {
                        model.isMember = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Memberid"].ToString() != "")
                {
                    model.Memberid = int.Parse(ds.Tables[0].Rows[0]["Memberid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_storeorder [where] 1=1 ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("" + strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        public List<Model.Stocks.Order> GetListOrderByWhere(string where)
        {
            throw new NotImplementedException();
        }

        public DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_storeorder [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion
        #endregion

        #region Other Function
        public IDbDataParameter[] ValueParas(YXShop.Model.Stocks.Order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_storeorder(");
            strSql.Append("id,ordernum,storeid,ordertime,adminid,isMember,Memberid,State)");
            strSql.Append(" values (");
            strSql.Append("@id,@ordernum,@storeid,@ordertime,@adminid,@isMember,@Memberid,@State)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@ordernum", SqlDbType.VarChar,20),
					new SqlParameter("@storeid", SqlDbType.Int,4),
					new SqlParameter("@ordertime", SqlDbType.DateTime),
					new SqlParameter("@adminid", SqlDbType.Int,4),
					new SqlParameter("@isMember", SqlDbType.Bit,1),
					new SqlParameter("@Memberid", SqlDbType.Int,4),
                    new SqlParameter("@State", SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Ordernum;
            parameters[2].Value = model.Storeid;
            parameters[3].Value = model.Ordertime;
            parameters[4].Value = model.Adminid;
            parameters[5].Value = model.isMember;
            parameters[6].Value = model.Memberid;
            parameters[7].Value = model.State;
            return parameters;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataSetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ordernum,storeid,ordertime,adminid,isMember,Memberid,State  ");
            strSql.Append(" FROM yxs_storeorder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLServerHelper.Query(strSql.ToString()); 
        }
        #endregion
    }
}
